library _;

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:flutter_yim/YIM.dart';
import 'package:pc/Const/IFinal.dart';
import 'package:pc/Model/UserModel.dart';
import 'package:pc/Page/Home/Chat/controller.dart';
import 'package:pc/Page/Home/Conversation/controller.dart';
import 'package:pc/Util/App/AppUtils.dart';
import 'package:pc/Util/MessageUtils.dart';
import 'package:pc/Util/WindowUtils.dart';
import 'package:pc/View/Loader.dart';
import 'package:window_manager/window_manager.dart';

part 'controller.dart';

part 'view.dart';

class HomePage extends YPage with WindowListener {
  UserModel? userModel;
  var badge_message_count = 0;

  ConversationController? controller_conversation;
  ChatController? controller_chat;

  @override
  void yOnCreate() async {
    if (!yIsWeb) {
      windowManager.addListener(this);
      await windowManager.setPreventClose(true);
    }
    IFinal.Others.homePage = this;
    userModel = await AppUtils.readUserModel();
    controller_conversation = ConversationController();
    controller_chat = ChatController();
    controller_conversation!.init();
    controller_chat!.init();
    controller_checkNetLines();
    controller_loginYIM();
    setState(() {});
  }

  @override
  void yOnDestory() {
    windowManager.removeListener(this);
    controller_conversation?.dispose();
    controller_chat?.dispose();
  }

  @override
  void onWindowClose() async {
    if (await windowManager.isPreventClose()) {
      await windowManager.hide(); //隐藏到托盘
    }
  }

  @override
  Widget yBuild(BuildContext context) {
    return Stack(
      children: [
        Row(
          mainAxisSize: MainAxisSize.max,
          children: [
            SizedBox(
              width: 65,
              height: yScreenSize().height,
              child: view_function(),
            ),
            Container(width: 0.1, height: yScreenSize().height, color: Colors.black),
            SizedBox(
              width: 260,
              height: yScreenSize().height,
              child: controller_conversation?.state.conversations == null ? Loader() : controller_conversation!.view.conversation(),
            ),
            Container(width: 0.1, height: yScreenSize().height, color: Colors.black),
            Expanded(
              child: SizedBox(
                height: yScreenSize().height,
                child: controller_chat?.state.messages == null ? Loader() : controller_chat!.view.chat(),
              ),
            ),
          ],
        ),
        controller_conversation?.state.conversations == null ? YEmpty() : controller_conversation!.view.searchResult(),
      ],
    );
  }
}
